package servlet.soft;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;

import javax.lang.model.element.VariableElement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import dao.SoftDao;
import dbutil.DBHelper;
import entity.Download;

public class AddDownloadServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6435917301886109466L;

	public AddDownloadServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter out = response.getWriter();
		JSONObject jsonObject = new JSONObject();
		String softName = request.getParameter("appName");
		String softPicUrl = request.getParameter("picUrl");
		String softLanguage = request.getParameter("language");
		String inner32 = request.getParameter("inner32");
		String inner64 = request.getParameter("inner64");
		String outer32 = request.getParameter("outer32");
		String outer64 = request.getParameter("outer64");
		String webDesc = request.getParameter("desc");
		Download download = null;
		SoftDao softDao = null;
		Connection conn = null;
		
		try {
			conn = DBHelper.getConnection();
			conn.setAutoCommit(false);
			softDao = new SoftDao();
			
			download = new Download();
			download.setAppName(softName);
			download.setAppPicURL(softPicUrl);
			download.setInner32(inner32);
			download.setInner64(inner64);
			download.setOuter32(outer32);
			download.setOuter64(outer64);
			download.setAppLanguage(softLanguage);
			download.setWebDesc(webDesc);
			
			boolean isSuccess = softDao.addDownload(download);
			
			
			if(isSuccess) {
				jsonObject.put("retCode", "1");
				conn.commit();
			} else {
				jsonObject.put("retCode", "0");
				jsonObject.put("desc", "AddDownLoadServlet dao");
				conn.rollback();
			}
			conn.setAutoCommit(true);
		} catch (SQLException e) {
			e.printStackTrace();
			jsonObject.put("retCode", "0");
			jsonObject.put("desc", " AddDownLoadServlet Exception");
		} finally {
			out.print(jsonObject.toString());
			out.close();
		}
	}

	public void init() throws ServletException {
	}

}
